#include "StdAfx.h"
#include "world.h"
#include "application.h"
#include "Project.h"

#include "navigationmanager.h"
#include "BasicCommandRes.h"
#include "ResourceInstance.h"

using namespace PDMS::PDMSFrame;
using namespace PDMS::PDMSDomain;
using namespace PDMS::PDMSUI;

extern NavigationManager* GetMember();

namespace PDMS
{
	namespace COMMAND
	{
		template<class T> class AddObjectCommand
		{
			public:
				AddObjectCommand();
				virtual ~AddObjectCommand();
			public:
				BOOL Execute();
		};

		template<class T> AddObjectCommand<T>::AddObjectCommand()
		{
		}

		template<class T> AddObjectCommand<T>::~AddObjectCommand()
		{
		}

		template<class T> BOOL AddObjectCommand<T>::Execute()
		{
			CResourceInstance rs;

			if( Application::Instance()->ActiveProject() == NULL )
			{
				StdString strTemp = "";
				strTemp.LoadString( IDC_WARRING_NOPROJECT );
				AfxMessageBox( strTemp );
				return FALSE;
			}
			
			UeDbObject* curObject = NULL; 
			UeObjectId curID = Application::Instance()->AcitveObject();
			
			Application::Instance()->ActiveProject()
				->GetCurDatabase()->GetObject(curID,curObject);
			ASSERT( curObject );
			T* pObject = new T;
			
			if( pObject->IsLegalParent( curObject ) )
			{
				UESoft::ErrorStatus es = pObject->SetParam();
				if( es == UESoft::eCANCEL )
				{
					curObject->Close();
		         	delete pObject;
	           		return FALSE;
				}
				
				curObject->Add( pObject );
				ActiveObjectListener activeObject( pObject->Id() );
				curObject->Close();
				pObject->Close();
				GetMember()->Updata();
				return TRUE;
			}
			CString strWar;
			strWar.LoadString( IDC_WARRING_NOCREATE );
			AfxMessageBox(strWar);
			curObject->Close();
			delete pObject;
			return FALSE;
		}
	}
}